package cn.test.dao;

import cn.test.entity.User;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sam on 2017/11/23.
 */
/*持久层实现类*/
@Repository("userDao")
public class UserDaoJdbcImpl implements UserDao {
    @Resource(name="ds")
    private DataSource ds;
    public User findByUsername(String uname) {
        User user = null;
        Connection conn = null;
        try {
            conn = ds.getConnection();
            String sql = "SELECT * FROM nbmission.sruser WHERE username = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,uname);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setId(rs.getString("id"));
                user.setUsername(uname);
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                user.setBirthday(rs.getDate("birthday"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            close(conn);
        }
        return user;
    }

    public void save(User user) {
        Connection conn = null;
        try {
            conn = ds.getConnection();
            String sql = "INSERT INTO nbmission.sruser VALUES(id,?,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getName());
            ps.setDate(4, user.getBirthday());
            ps.executeUpdate();
        } catch (SQLException e1) {
            e1.printStackTrace();
            throw new RuntimeException(e1);
        } finally {
            try {
                conn.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
                throw new RuntimeException(e1);
            }
        }
    }

    public void updateById(User user) {
        Connection conn = null;
        try {
            conn = ds.getConnection();
            String sql = "UPDATE nbmission.sruser SET username=?,password=?,name=?,birthday=? WHERE id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getName());
            ps.setDate(4, user.getBirthday());
            ps.setString(5,user.getId());
            ps.executeUpdate();
        } catch (SQLException e1) {
            e1.printStackTrace();
            throw new RuntimeException(e1);
        } finally {
            try {
                conn.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
                throw new RuntimeException(e1);
            }
        }
    }

    public List<User> findAll() {
        List<User> users = new ArrayList<User>();
        Connection conn = null;
        try {
            conn = ds.getConnection();
            String sql = "select * from nbmission.sruser";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                User u = new User();
                u.setId(rs.getString("id"));
                u.setUsername(rs.getString("username"));
                u.setPassword(rs.getString("password"));
                u.setName(rs.getString("name"));
                u.setBirthday(rs.getDate("birthday"));
                users.add(u);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return users;
    }

    public User findById(String id) {
        User user = null;
        Connection conn = null;
        try {
            conn = ds.getConnection();
            String sql = "SELECT * FROM nbmission.sruser WHERE id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setId(id);
                user.setUsername(rs.getString("username"));
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                user.setBirthday(rs.getDate("birthday"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            close(conn);
        }
        return user;
    }

    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
}
